<?php

namespace App\Services\Alipay;

use Exception;
use Illuminate\Support\Str;

class AlipayAppPay
{
    private $client = null;
    private $request = null;
    private $params = [
        'product_code'=> 'QUICK_MSECURITY_PAY'
    ];

    public $rsaPublicKey = null;

    public function __construct($client, $request)
    {
        $this->client = $client;
        $this->request = $request;
    }

    public function getPayPara()
    {
        $bizcontent = json_encode($this->params);
        $this->request->setBizContent($bizcontent);

        $response = $this->client->sdkExecute($this->request);

        return $response;

        // echo htmlspecialchars($response);//就是orderString 可以直接给客户端请求，无需再做处理。
    }

    public function setTotalFee($value)
    {
        $this->params['total_amount'] = $value;
        return $this;
    }

    public function verify()
    {
        if (empty($_POST) && empty($_GET)) {
            return false;
        }

        $data = $_POST ?: $_GET;

        return $this->client->rsaCheckV1($data, $this->rsaPublicKey, $this->client->signType);
    }

    // $bizcontent = "{\"body\":\"我是测试数据\"," 
    //                 . "\"subject\": \"App支付测试\","
    //                 . "\"out_trade_no\": \"20170125test01\","
    //                 . "\"timeout_express\": \"30m\"," 
    //                 . "\"total_amount\": \"0.01\","
    //                 . "\"product_code\":\"QUICK_MSECURITY_PAY\""
    //                 . "}";
    // $request->setBizContent($bizcontent);

    public function __call($method, $args)
    {
        $key = Str::substr($method, 3);
        $key = Str::snake($key);
        $this->params[$key] = $args[0];
        return $this;
    }
}